package nl.infodation.emr.pricingservice.ui;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import java.util.List;

import nl.infodation.emr.pricingservice.controller.ControllerFactory;
import nl.infodation.emr.pricingservice.entity.SystemValue;
import nl.infodation.emr.pricingservice.entity.SystemValue.TableName;
import nl.infodation.emr.pricingservice.ui.component.PagingComponent;
import nl.infodation.emr.pricingservice.ui.component.PagingModel;
import nl.infodation.emr.pricingservice.ui.page.SessionData;
import nl.infodation.emr.utility.Constants;

import com.vaadin.annotations.AutoGenerated;
import com.vaadin.data.util.BeanItem;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.Label;
import com.vaadin.ui.VerticalLayout;

@SuppressWarnings({ "rawtypes", "unchecked" })
public class CountryCheckerPopup extends CustomComponent implements
		ClickListener {
	private static final long serialVersionUID = 1L;

	private VerticalLayout mainLayout;
	private VerticalLayout verticalLayout;
	private Button btnOk;
	private CountryCheckerList countryCheckerList;

	private Label lblCurrentPage;
	private PagingComponent pagingComponent;
	private PagingModel model = new PagingModel(0);

	private String action;
	private ActiveDomainRuleListView parentView;

	public CountryCheckerPopup(ActiveDomainRuleListView parentView,
			String action) {
		this.parentView = parentView;
		this.action = action;
		buildMainLayout();
		setCompositionRoot(mainLayout);
		btnOk.addListener((ClickListener) this);
		initialModelPaging();
	}

	private void initialModelPaging() {
		setTotalNumberOfPagesForReload();

		model.addPropertyChangeListener("currentPage",
				new PropertyChangeListener() {
					public void propertyChange(final PropertyChangeEvent evt) {
						updatePageNumber();
						countryCheckerList.loadPage(model.getCurrentPage());
					}
				});
		pagingComponent.setModel(model);
		updatePageNumber();
	}

	private void updatePageNumber() {
		lblCurrentPage.setValue(SessionData.getI18N().getMessage(
				Constants.CURRENT_PAGE_VALUE)
				+ model.getCurrentPage());
	}

	private void setTotalNumberOfPagesForReload() {
		List<BeanItem> items = new ArrayList<BeanItem>();
		if (SessionData.isSuperAdmin() || SessionData.isNormalAdmin()) {

			SystemValue systemValue = new SystemValue();
			systemValue.setSystemName(TableName.COUNTRY);
			items.addAll(ControllerFactory.getSystemValueController()
					.getPaginationByCriteria(systemValue, 1, Integer.MAX_VALUE));

		} else {
			for (SystemValue country : SessionData.getCountriesToSee()) {
				BeanItem item = new BeanItem(country);
				items.add(item);
			}
		}

		final long totalItems = items.size();
		final int totalPages = (int) Math.ceil(totalItems
				/ (float) countryCheckerList.getPageSize());
		model.setTotalNumberOfPages(totalPages);
	}

	@Override
	public void buttonClick(com.vaadin.ui.Button.ClickEvent event) {
		final Button source = event.getButton();
		if (source == btnOk) {
			if (Constants.ACTION_SHOW_LOADED_RULES.equals(action)) {
				parentView.executeShowLoadedRules(false, true,
						parentView.getCountriesFromCountryCheckerList());
			} else if (Constants.ACTION_MARK_LOADED.equals(action)) {
				parentView.executeShowLoadedRules(true, true,
						parentView.getCountriesFromCountryCheckerList());
			} else if (Constants.ACTION_RELOAD_RULES.equals(action)) {
				parentView.executeReloadRule(parentView
						.getCountriesFromCountryCheckerList());
			} else if (Constants.ACTION_EXECUTE_SCRIPT.equals(action)) {
				parentView.confirmDeploy(ActiveDomainRuleListView.CONFIRM_EXECUTE_SCRIPT);
			}
		}
	}

	@AutoGenerated
	private VerticalLayout buildMainLayout() {
		// common part: create layout
		mainLayout = new VerticalLayout();
		mainLayout.setImmediate(Boolean.FALSE);
		mainLayout.setWidth("300px");
		mainLayout.setHeight("340px");
		mainLayout.setMargin(Boolean.FALSE);

		// top-level component properties
		setWidth("300px");
		setHeight("500px");

		// countryCheckerList_1
		countryCheckerList = new CountryCheckerList();
		countryCheckerList.setImmediate(Boolean.FALSE);
		countryCheckerList.setWidth("100.0%");
		countryCheckerList.setHeight("200px");
		countryCheckerList.setStyleName("countrypopup");
		mainLayout.addComponent(countryCheckerList);

		// verticalLayout_1
		verticalLayout = buildVerticalLayout();
		mainLayout.addComponent(verticalLayout);

		return mainLayout;
	}

	private VerticalLayout buildVerticalLayout() {
		// common part: create layout
		verticalLayout = new VerticalLayout();
		verticalLayout.setImmediate(Boolean.FALSE);
		verticalLayout.setWidth("100.0%");
		verticalLayout.setHeight("115px");
		verticalLayout.setMargin(Boolean.FALSE);

		// pagingComponent_1
		pagingComponent = new PagingComponent();
		pagingComponent.setImmediate(Boolean.FALSE);
		verticalLayout.addComponent(pagingComponent);

		// lblCurrentPage
		lblCurrentPage = new Label();
		lblCurrentPage.setImmediate(Boolean.FALSE);
		lblCurrentPage.setValue("Current Page: ?");
		verticalLayout.addComponent(lblCurrentPage);

		// btnOk
		btnOk = new Button();
		btnOk.setCaption(SessionData.getI18N().getMessage(Constants.OK_BUTTON));
		btnOk.setImmediate(Boolean.TRUE);
		btnOk.setWidth("-1px");
		btnOk.setHeight("-1px");
		verticalLayout.addComponent(btnOk);

		return verticalLayout;
	}

}